var ctx=window.daijia.ctx;
var zhuancheTotalElements=window.daijia.totalElements;

$(document).ready(function(){
	
	var znewDate = new Date();
	var zstartDate = new Date();
	zstartDate.setDate(zstartDate.getDate()-30);
	var zstartTimeStr=zstartDate.getFullYear()+"-"+((zstartDate.getMonth()+1)>9?(zstartDate.getMonth()+1):'0' + (zstartDate.getMonth()+1))+"-"+(zstartDate.getDate()>9?zstartDate.getDate():'0' + zstartDate.getDate());
	var zendTimeStr=znewDate.getFullYear()+"-"+((znewDate.getMonth()+1)>9?(znewDate.getMonth()+1):'0' + (znewDate.getMonth()+1))+"-"+(znewDate.getDate()>9?znewDate.getDate():'0' + znewDate.getDate());
	
	$("#zhuancheStartTime").val(zstartTimeStr);
	$("#zhuancheEndTime").val(zendTimeStr);
	$("#zhuancheDateRangeText").text($("#zhuancheStartTime").val()+" 至 "+$("#zhuancheEndTime").val());
	
	zhuancheInitPagedata(zhuancheTotalElements);
	
	//点击选择时间
	$("#zhuancheDateRangeButton").click(function(){
		var dropdowncalendar=$(this).parent().find(".dropdown-calendar");
		if(dropdowncalendar.is(':hidden')){
			dropdowncalendar.show();
			if(dropdowncalendar.find(".datepicker").length==0){
				$('#zhuancheDateRangeCalendar').DatePicker({
					flat: true,
					date: [zstartDate,znewDate],
					current: znewDate,
					calendars: 3,
					mode: 'range',
					starts: 1,
					onChange: function(formated, dates){
						$("#zhuancheStartTime").val(formated[0]);
						$("#zhuancheEndTime").val(formated[1]);
					}
				});
			}
		}
		else{
			dropdowncalendar.hide();
		}
	});
	
	//点击应用
	$("#zhuancheApplyIt").click(function(){
		$("#zhuanche_search_button").click();
	});

	//点击搜索
	$("#zhuanche_search_button").click(function(){
		if($("#zhuancheStartTime").val()=="" || $("#zhuancheEndTime").val()==""){
			$.scojs_message("日期不能为空", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1}$/;
		if(!DATE_FORMAT.test($("#zhuancheStartTime").val()) || !DATE_FORMAT.test($("#zhuancheEndTime").val())){
		   $.scojs_message("您输入的日期格式有误，正确格式应为：2012-01-01", $.scojs_message.TYPE_ERROR);
		   return false;
		}
		if($("#zhuancheStartTime").val()>$("#zhuancheEndTime").val()){
			 $.scojs_message("开始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
			 return false;
		}
		
		$("#zhuancheDateRangeText").text($("#zhuancheStartTime").val()+" 至 "+$("#zhuancheEndTime").val());
		$("#zhuancheDateRangeButton").parent().find(".dropdown-calendar").hide();
		
		var newTotalElements=getZhuancheTotalElements();
		zhuancheInitPagedata(newTotalElements);
		$(".pageshow").each(function(index,element){
			$(element).find("li a").html("共 "+newTotalElements+" 名推荐人");
		});
	});
	
	//导出。。。。。。。。
	//点击选择时间
	$("#exportDateRangeButton").click(function(){
		var dropdowncalendar=$(this).parent().find(".dropdown-calendar");
		if(dropdowncalendar.is(':hidden')){
			dropdowncalendar.show();
			if(dropdowncalendar.find(".datepicker").length==0){
				$('#exportDateRangeCalendar').DatePicker({
					flat: true,
					date: newDate,
					current: newDate,
					calendars: 3,
					mode: 'range',
					starts: 1,
					onChange: function(formated, dates){
						$("#exportStartTime").val(formated[0]);
						$("#exportEndTime").val(formated[1]);
					}
				});
			}
		}
		else{
			dropdowncalendar.hide();
		}
	});
	
	//点击应用
	$("#exportApplyIt").click(function(){
		if($("#exportStartTime").val()=="" && $("#exportEndTime").val()==""){
			$("#exportDateRangeText").text("请选择");
		}
		else{
			$("#exportDateRangeText").text($("#exportStartTime").val()+" 至 "+$("#exportEndTime").val());
		}
		$("#exportDateRangeButton").parent().find(".dropdown-calendar").hide();
	});
	
	//点击清空
	$("#exportClearIt").click(function(){
		$("#exportStartTime").val("");
		$("#exportEndTime").val("");
		$('#exportDateRangeCalendar').DatePickerClear();
		$("#exportApplyIt").click();
	});
	
	//导出数量
	$("#allAmount").click(function(){
		if($(this).is(':checked')){
			$("#amounts").attr("disabled",true);
		}
		else{
			$("#amounts").attr("disabled",false);
		}
	});
	
	//开始导出
	$("#export_button").click(function(){
		
		if(!$("#allAmount").is(':checked')){
			var amounts=$("#amounts").val();
			if(amounts==""){
				$.scojs_message("请输入导出数量", $.scojs_message.TYPE_ERROR);
				return false;
			}
			else if(amounts<=0){
				$.scojs_message("导出数量最少为1条", $.scojs_message.TYPE_ERROR);
				return false;
			}
		}
		
		window.location=ctx+"/chart/exportIntroduce?"+$("#ExportForm").serialize();
	});
	
});

function zhuancheLoadData(page){
	
	if($('#zhuancheloadblacklayer').length==0){
		var dw = $(".table").width();
		var dh = $(".table").height();
		var backlayer ="<div id='zhuancheloadblacklayer'><div style='margin-top:"+(dh/2)+"px;margin-left:"+(dw/2)+"px;'><img src='"+ctx+"/assets/css/images/loading.gif' /></div></div>"
		$("#zhuancheTable").append(backlayer);
		$('#zhuancheloadblacklayer').css({"z-index":"9","background":"#000","opacity":"0.5","position":'absolute',"top":0,"width":dw,"height":dh});
	}
	
	var off_set=(page-1)*10;
	var newcontent = '';
	var newTotalElements=0;
	
	$.ajax({
        url : ctx+'/chart/zhuancheIntroduceData',
        type:'GET',
        async:true,
        dataType : 'json',
        data : {
        	"pager.offset":off_set,
        	"startTime":$("#zhuancheStartTime").val(),
        	"endTime":$("#zhuancheEndTime").val(),
        	"param":$("#zhuancheParam").val(),
        	"companyId":$("#zhuancheCompanyId").val()
        },
        success : function(result){
        	if(result.totalElements>0){/*
        newcontent+="<tbody style='display:none;'>";
        		$(result.content).each(function(index,data){
        			var name="";
        			if(data.name!="" && data.username!=""){
        				name=data.name+"("+data.username+")";
        			}
	    newcontent+="<tr>"+
	    				"<td>"+data.introduced+"</td>"+
						"<td>"+name+"</td>"+
						"<td>"+data.type+"</td>"+
						"<td>"+data.companyName+"</td>"+
						"<td>"+data.finishNums+"</td>"+
						"<td><span style='color:#ff950b;font-weight:700;'>"+data.money+"</span></td>"+
						"<td><span style='color:#ff950b;font-weight:700;'>"+data.ticheng+"</span></td>"+
						"<td><span style='color:#ff950b;font-weight:700;'>"+data.virtual+"</span></td>"+
					"</tr>";
        		});
        newcontent+="</tbody>";
        	*/
        	var content = result.content;
        	newcontent+="";
        	for(var i=0;i<content.length;i++){
        		var driver = content[i].drivers;
        		
        	 if(driver!=null){
        		 var row = driver.length;
         		newcontent+="<tr>"+
 				"<td rowspan='"+row+"'>"+content[i].introduced+"</td>";
         		for(var j=0;j<row;j++){
         			var name="";
         			if(driver[j].name!="" && driver[j].username!=""){
         				name=driver[j].name+"("+driver[j].username+")";
         			}
         			newcontent+=
     					"<td>"+name+"</td>"+
     					"<td>"+driver[j].finishNums+"</td>"+
     					"<td><span style='color:#ff950b;font-weight:700;'>"+driver[j].money+"</span></td>"+
     					"<td><span style='color:#ff950b;font-weight:700;'>"+driver[j].ticheng+"</span></td>"+
     				"</tr>";
         			
         		};
        		 
        	 }else{
         		newcontent+="<tr>"+
 				"<td rowspan='"+0+"'>"+"无"+"</td>";
         			var name="";
         			if(content[i].name!="" && content[i].username!=""){
         				name=content[i].name+"("+content[i].username+")";
         			}
         			newcontent+=
     					"<td>"+name+"</td>"+
     					"<td>"+content[i].finishNums+"</td>"+
     					"<td><span style='color:#ff950b;font-weight:700;'>"+content[i].money+"</span></td>"+
     					"<td><span style='color:#ff950b;font-weight:700;'>"+content[i].ticheng+"</span></td>"+
     				"</tr>";
        		 
        	 }
        		
        	}
        	
        	}
        	newcontent="<tbody style='display:none;'>"+newcontent+"</tbody>";
        	$('#zhuancheloadblacklayer').remove();
        	$("#zhuancheTable tbody").remove();
        	
        	$("#zhuancheTable").append(newcontent);
        	$("#zhuancheTable tbody").show(300);
    		newTotalElements=result.totalElements;
        }
	});
	
	return newTotalElements;
}

//分页初始化
function zhuancheInitPagedata(total){
	$(".pagedata").paging(total, {
		format: '[< ncnnn >]',
		perpage: 10,
		onSelect: function (page) {
			zhuancheLoadData(page);
	    	return false;
		},
		onFormat: function (type) {
			switch (type) {
			case 'block': // n and c
				if (!this.active)
					return '';
				else if (this.value != this.page)
					return '<li><a href="#">' + this.value + '</a></li>';
				return '<li class="active"><a href="#">' + this.value + '</a></li>';
			case 'next': // >
				return '<li><a href="#">&rsaquo;</a><li>';
			case 'prev': // <
					return '<li><a href="#">&lsaquo;</a><li>';
			case 'first': // [
					return '<li><a href="#">&laquo;</a><li>';
			case 'last': // ]
					return '<li><a href="#">&raquo;</a></li>';
			}
		}
	});
}

function getZhuancheTotalElements(){
	
	$.ajax({
        url : ctx+'/chart/zhuancheIntroduceTotal',
        type:'GET',
        async:false,
        dataType : 'json',
        data : {
        	"companyId":$("#zhuancheCompanyId").val(),
        	"param":$("#zhuancheParam").val()
        },
        success : function(result){
        	newTotalElements=result;
        }
	});
	
	return newTotalElements;
}
	

